Overview

Row

Global sleep overview (3 subjects Night Owl Early Bird Sleepy Bear)

y variable defined on the left remains the same

Row

total sleep overview

Row

total sleep overview (by day of week)

deep sleep variation (by day of week)

Row

_stress_awake (by day of week)

_stress_awake

Corr_Night analysis

Row

Influence from Median_hr_sleep on sleep quality

Influence from median_stress_sleep on sleep quality

Row

Correlation stress & HR

The p-value of the test is less than the significance level alpha = 0.05. We can conclude that hr sleep and stress sleep are significantly correlated with a correlation coefficient beetween 0.64–0.88.

Correlation vigorous_activity_hours & total_sleep_hours

The p-value of the test is more than the significance level alpha = 0.05. We can conclude that variable are not significantly correlated .

Correlation steps & total_sleep_hours

The p-value of the test is more than the significance level alpha = 0.05. We can conclude that variable are not significantly correlated .

Row

**Correlation %_high_stress_30min_before_bedtime & total_sleep_hours**

The p-value of the test is more than the significance level alpha = 0.05. We can conclude that variable are not significantly correlated .

Correlation estimated_sleep_time & total_sleep_hours

The p-value of the test is less (2 participants) than the significance level alpha = 0.05. We can conclude that variable are significantly correlated .

Global corr analysis & ML Models

Row

Correlation

Row

ML decision tree

n= 122 

node), split, n, loss, yval, (yprob)
      * denotes terminal node

 1) root 122 24 0 (0.80327869 0.19672131)  
   2) total_sleep_hours>=7.233333 89 10 0 (0.88764045 0.11235955)  
     4) light_sleep_percentage< 0.7336932 82  6 0 (0.92682927 0.07317073)  
       8) light_sleep_percentage>=0.547778 68  2 0 (0.97058824 0.02941176) *
       9) light_sleep_percentage< 0.547778 14  4 0 (0.71428571 0.28571429)  
        18) awake_percentage< 0.0137832 10  1 0 (0.90000000 0.10000000) *
        19) awake_percentage>=0.0137832 4  1 1 (0.25000000 0.75000000) *
     5) light_sleep_percentage>=0.7336932 7  3 1 (0.42857143 0.57142857) *
   3) total_sleep_hours< 7.233333 33 14 0 (0.57575758 0.42424242)  
     6) light_sleep_percentage< 0.5924029 15  2 0 (0.86666667 0.13333333) *
     7) light_sleep_percentage>=0.5924029 18  6 1 (0.33333333 0.66666667)  
      14) %_stress_awake>=0.4945208 8  2 0 (0.75000000 0.25000000) *
      15) %_stress_awake< 0.4945208 10  0 1 (0.00000000 1.00000000) *

Row

---
title: "Sleep Dashboard "
output: 
  flexdashboard::flex_dashboard:
    orientation: rows
    vertical_layout: scroll
    theme: 
      bg: "#ffffff"
      fg: "#000000" 
      primary: "#ED79F9"
    logo: logo.png
    source_code: embed
---

```{r setup, include=FALSE}
library(flexdashboard)
library(readr)
library(tidyverse)
library(hrbrthemes)
library(viridis)
library(lubridate)
library(reshape2)
library(ggplot2)
library(ggpubr)
library(plotly)
library(GGally)
setwd("~/Desktop/module_3/all_data")

Lorena <- read_csv("Lorena_converted (3).csv")
Lorena$ID<-"W1"
Martin <- read_csv2("Martin.csv")
Martin$ID<-"M1"
Nely <- read_csv2("Nely.csv")
Nely$ID<-"W2"
dd<-merge(Nely, Martin, all = TRUE)
dd<-merge(dd, Lorena, all = TRUE)

```

Overview
=======================================================================

Row
-----------------------------------------------------------------------

###  Global sleep overview (3 subjects Night Owl Early Bird Sleepy Bear)

```{r}
dd1<-dd[,c(1,11,12,13,14,98)]
dd1<-as.data.frame(dd1)
dd1<-melt(dd1, id=c("calendar_date","ID"))
p<-ggplot(dd1, aes(x=calendar_date,y=round(value*100),group=variable, fill=variable)) +
  geom_density(stat = "identity",alpha=.6)+theme_ipsum() +scale_fill_ipsum()+
  labs(x = "Date",
       y = "Sleep in %",
       color = "Sleep variable")+facet_grid(~ID)
ggplotly(p)%>% layout(legend = list(orientation = "h", y = -0.2))
```
>y variable defined on the left remains the same

Row
-----------------------------------------------------------------------

### total sleep overview

```{r }
p<-ggplot(dd, aes(x = calendar_date, y = total_sleep_hours)) +
  geom_line()+geom_point()+geom_smooth()+theme_ipsum()+facet_grid(~ID)
ggplotly(p)%>% layout(legend = list(orientation = "h", y = -0.2))
```


Row
-----------------------------------------------------------------------

### total sleep overview (by day of week)

```{r }
dd$day<-weekdays(dd$calendar_date,abbreviate = TRUE)  
dd$day<-as.factor(dd$day)
dd$day2<-dd$day
levels(dd$day2) <- c("Sunday","Thursday", "Monday", "Tuesday", "Wednesday", "Saturday", 
             "Friday")
dd$day2 <- ordered(dd$day2, levels =c("Monday","Tuesday", "Wednesday","Thursday", "Friday",  "Saturday", "Sunday"))
p<-dd %>%
  ggplot( aes(x=day2,y = total_sleep_hours,fill=ID)) +
  geom_boxplot() + theme_ipsum()  +scale_fill_ipsum()
p %>%
  ggplotly() %>%
  layout(boxmode = "group")
```

###  deep sleep variation  (by day of week)

```{r }
p<-dd %>%
  ggplot( aes(x=day2,y = deep_sleep_percentage,fill=ID)) +
  geom_boxplot() + theme_ipsum()  +scale_fill_ipsum()
p %>%
  ggplotly() %>%
  layout(boxmode = "group")

```


Row
-----------------------------------------------------------------------


###  _stress_awake  (by day of week)

```{r }
p<-dd %>%
  ggplot( aes(x=day2,y = dd$`%_stress_awake`,fill=ID)) +
  geom_boxplot() + theme_ipsum()  +scale_fill_ipsum()
p %>%
  ggplotly() %>%
  layout(boxmode = "group")

```

###  _stress_awake  

```{r }
p<-ggplot(dd, aes(x=calendar_date,y=round(median_stress_awake))) +
  geom_smooth()+facet_grid(~ID)+theme_ipsum()

  ggplotly(p) 

```


Corr_Night analysis 
=======================================================================

Row 
-----------------------------------------------------------------------

### **Influence from Median_hr_sleep on sleep quality**

```{r }

p<-  ggplot(dd,aes(x=perceived_sleep_quality, y=median_hr_sleep, fill=factor(ID))) +
  geom_boxplot() +geom_point()+
  scale_fill_ipsum()+
  labs(x = "perceived_sleep_quality by subject",
       y = "median_hr_sleep",
       color = "Sleep variable")+ theme_ipsum() +scale_fill_ipsum()
ggplotly(p)%>%
  layout(boxmode = "group")
```

### **Influence from median_stress_sleep on sleep quality**

```{r}

p<-  ggplot(dd,aes(x=perceived_sleep_quality, y=median_stress_sleep, fill=factor(perceived_sleep_quality))) +
  geom_boxplot() +geom_point()+
  scale_fill_ipsum()+
  labs(x = "perceived_sleep_quality",
       y = "median_stress_sleep",
       color = "Sleep variable")+ theme_ipsum() +scale_fill_ipsum()
ggplotly(p)%>% layout(legend = list(orientation = "h", y = -0.2))

```



Row 
-----------------------------------------------------------------------


### **Correlation  stress & HR**

```{r}

p<-ggscatter(dd, x = "median_stress_sleep", y = "median_hr_sleep", color="ID",
          add = "reg.line", conf.int = FALSE, 
          cor.coef = FALSE, cor.method = "pearson")+
  stat_cor(aes(color = ID), p.accuracy = 0.001, r.accuracy = 0.01)+ theme_ipsum()+scale_fill_ipsum()
p
```

> The p-value of the test is less than the significance level alpha = 0.05. We can conclude that hr sleep and stress sleep are significantly correlated with a correlation coefficient beetween 0.64--0.88.

### **Correlation vigorous_activity_hours & total_sleep_hours**

```{r}

p<-ggscatter(dd, x = "vigorous_activity_hours", y = "total_sleep_hours", color="ID",
          add = "reg.line", conf.int = FALSE, 
          cor.coef = FALSE, cor.method = "pearson")+
  stat_cor(aes(color = ID), p.accuracy = 0.001, r.accuracy = 0.01)+ theme_ipsum()+scale_fill_ipsum()
p
```

>The p-value of the test is more than the significance level alpha = 0.05. We can conclude that variable are not significantly correlated .

### **Correlation steps & total_sleep_hours**

```{r}

p<-ggscatter(dd, x = "steps", y = "total_sleep_hours", color="ID",
          add = "reg.line", conf.int = FALSE, 
          cor.coef = FALSE, cor.method = "pearson")+
  stat_cor(aes(color = ID), p.accuracy = 0.001, r.accuracy = 0.01)+ theme_ipsum()+scale_fill_ipsum()
p
```

>The p-value of the test is more than the significance level alpha = 0.05. We can conclude that variable are not significantly correlated .


Row 
-----------------------------------------------------------------------

### **Correlation %_high_stress_30min_before_bedtime & total_sleep_hours**

```{r}

p<-ggscatter(dd, x = "%_high_stress_30min_before_bedtime", y = "total_sleep_hours", color="ID",
          add = "reg.line", conf.int = FALSE, 
          cor.coef = FALSE, cor.method = "pearson")+
  stat_cor(aes(color = ID), p.accuracy = 0.001, r.accuracy = 0.01)+ theme_ipsum()+scale_fill_ipsum()
p
```

>The p-value of the test is more than the significance level alpha = 0.05. We can conclude that variable are not significantly correlated .

### **Correlation estimated_sleep_time & total_sleep_hours**

```{r}

p<-ggscatter(dd, x = "estimated_sleep_time", y = "total_sleep_hours", color="ID",
          add = "reg.line", conf.int = FALSE, 
          cor.coef = FALSE, cor.method = "pearson")+
  stat_cor(aes(color = ID), p.accuracy = 0.001, r.accuracy = 0.01)+ theme_ipsum()+scale_fill_ipsum()
p
```

>The p-value of the test is less (2 participants) than the significance level alpha = 0.05. We can conclude that variable are significantly correlated .

Global corr analysis & ML Models
=======================================================================



Row 
-----------------------------------------------------------------------

### **Correlation **

```{r }

dd2<-dd[,c(6,42,69,97)]
ggpairs(dd2)
```

Row {}
-----------------------------------------------------------------------

### **ML decision tree**


```{r message=FALSE, warning=FALSE}
library(rpart)
library(rpart.plot) 
library(Metrics)
## Trainingfsfunktion
## für Visualisierung (prp)
## Performance Metriken (accuracy)
#Test - Train - Split (Details dazu später)
data<-dd
##Variable selection
data<-dd[,c(6,11:16,40:47,69,70,92,97)]
data$bad_sleep_outlier_binary<-as.factor(data$bad_sleep_outlier_binary)
#train & test model
in_train <- sample(nrow(data), trunc(nrow(data) * 2 / 3)) 
train <- data[in_train,]
test <- data[-in_train,]
# Model formul
L_form <- bad_sleep_outlier_binary ~ .
model <- rpart(L_form, train,method = "class", minsplit = 10, minbucket = 3)
model
prp(model)

rpart.plot(model, extra= 106)


```

Row {}
-----------------------------------------------------------------------

### **ML using glm(  with bad_sleep_outlier_binary ~ ., family = binomial(link = "logit")**


```{r message=FALSE, warning=FALSE}
library(sjPlot)
library(sjmisc)
library(sjlabelled)
dd1<-na.omit(data)
in_train <- sample(nrow(dd1), trunc(nrow(dd1) * 2 / 3)) 
train <- dd1[in_train,]
test <- dd1[-in_train,]
dd1$bad_sleep_outlier_binary<-as.factor((dd1$bad_sleep_outlier_binary))
model <- glm(bad_sleep_outlier_binary ~.,family=binomial(link='logit'),data=train)
summary(model)
tab_model(model,show.intercept = TRUE)

```